package com.data.platform.core.model;

import com.data.platform.core.enums.ResponseCode;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 响应
 * @param <T>
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class R<T> implements Serializable {
    private static final long serialVersionUID = 532610602095653564L;

    private Integer code;

    private String message;

    private T data;

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Long total;

    R(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> R<T> success(){
        return new R<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage());
    }

    public static <T> R<T> success(T data){
        return new R<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data, null);
    }

    public static <T> R<T> success(String message, T data){
        return new R<>(ResponseCode.SUCCESS.getCode(), message, data, null);
    }

    public static <T> R<T> success(Long total, T data){
        return new R<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data, total);
    }

    public static <T> R<T> success(Long total, String message, T data){
        return new R<>(ResponseCode.SUCCESS.getCode(), message, data, total);
    }

    public static <T> R<T> error(){
        return new R<>(ResponseCode.SERVER_ERROR.getCode(), ResponseCode.SERVER_ERROR.getMessage());
    }

    public static <T> R<T> error(String message){
        return new R<>(ResponseCode.EXCEPTION.getCode(), message);
    }

    public static <T> R<T> error(Integer code, String message){
        return new R<>(code, message);
    }

    public static <T> R<T> error(ResponseCode responseCode){
        return new R<>(responseCode.getCode(), responseCode.getMessage());
    }
}
